//
// Created by 123 on 2024/3/7.
//前缀表达式的计算


#include <iostream>

#include "../0.utils/Int_Stack.cpp"

using namespace std;
//前缀表达式的计算
int compute(string str);

//是否是符号
bool isOpe(char ch);

//计算
int calculate(int a, int b, char ch);

int main(){


    string str="-*/5-7+113+2+11";
    cout<<"计算结果是："<<compute(str);


}



int compute(string str){
    LinkStack stack=init_IntStack();
    for (int i = str.length()-1; i >=0 ; --i) {
        char ch=str[i];
       if(!isOpe(ch)){
           int a=ch-48;
           push(stack,a);
       }else{
           int a=pop(stack);
           int b=pop(stack);
           push(stack, calculate(a,b,ch));
       }
    }
    return pop(stack);
}





bool isOpe(char ch) {
    return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int calculate(int a, int b, char ch) {
    if (ch == '+') {
        return a + b;
    } else if (ch == '-') {
        return a - b;
    } else if (ch == '*') {
        return a * b;
    } else {
        return a / b;
    }
}

int getOpeNum(char a) {
    if (a == '*' || a == '/')
        return 1;
    else
        return 0;
}


